package com.yiyao.code;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.zxing.activity.CaptureActivity;

public class Dingdan extends Activity {

	ListView listview;
	TextView btnquery;
	Button scanBarCodeButton;
	MyAdapter adapter;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				Intent intent = new Intent();
				intent.setClass(Dingdan.this, DingdanList.class);
				startActivity(intent);
				break;
			case 2:
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dingdan);
		initUI();
		init();
		initListener();

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private void init() {
		adapter = new MyAdapter(this);
		listview.setAdapter(adapter);
	}

	private void initUI() {
		scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
		listview = (ListView) findViewById(R.id.main_tab_container);
		btnquery = (TextView) findViewById(R.id.textView2);
	}

	private void initListener() {
		btnquery.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				handler.sendEmptyMessage(1);
			}
		});
		scanBarCodeButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent openCameraIntent = new Intent(Dingdan.this,
						CaptureActivity.class);
				startActivityForResult(openCameraIntent, 0);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			adapter.add(scanResult);
		}
	}

	class MyAdapter extends BaseAdapter {
		List<String> mylist;
		LayoutInflater layouInflater;

		public MyAdapter(Context context) {
			mylist = new ArrayList<String>();
			layouInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mylist.size();
		}

		public void add(String scanResult) {
			mylist.add(scanResult);
			notifyDataSetChanged();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = layouInflater.inflate(R.layout.item, null);
				holder.title = (TextView) convertView.findViewById(R.id.name);
				holder.number = (EditText) convertView
						.findViewById(R.id.number);
				convertView.setTag(holder);
			}else{
				holder  = (ViewHolder)convertView.getTag();  
			}
			holder.title.setText(mylist.get(position));
			InputFilter lengthfilter = new InputFilter() {

				@Override
				public CharSequence filter(CharSequence source, int start,
						int end, Spanned dest, int dstart, int dend) {
					
					try{
					Integer.parseInt(dest.toString());
					if(dest.toString().length()>2){
						 return source.subSequence(start, end -2);   
					}
					}catch(Exception e){
						return null;
					}
					return null;
				}   
			};
			holder.number.setFilters(new InputFilter[]{lengthfilter });
			return convertView;
		}

		public final class ViewHolder {
			public TextView title;
			public EditText number;
		}

	}
}
